package fr.adfm.database.initialization;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import fr.adfm.entity.Acheteur;
import fr.adfm.entity.Administrateur;
import fr.adfm.entity.Adresse;
import fr.adfm.entity.Client;
import fr.adfm.entity.Comptable;
import fr.adfm.entity.Fournisseur;
import fr.adfm.entity.Vendeur;

@Component
public class DbCreateDataForTesting {
	
	protected EntityManager em;

	    	
	@PersistenceContext(unitName="entityManagerFactory")
	public void setEntityManager(EntityManager entityManager) {
		this.em = entityManager;
	}
	
	@Transactional
	public void CreateMembers() {
		
		Adresse adresse1 = new Adresse("nom1","prenom1","raisonSociale1","rue1 1","rue2 1",783690,"ville1");
		Adresse adresse2 = new Adresse("nom2","prenom2","raisonSociale2","rue1 2","rue2 2",783690,"ville2");
		
		List<Adresse> clientAdresses = new ArrayList<Adresse>();
		 
		clientAdresses.add(adresse1);
		clientAdresses.add(adresse2);
		
		Client client1 = new Client("mdp4", "adresse@4", "nom4", "telephone4");
		Client client2 = new Client("mdp5", "adresse@5", "nom5", "telephone5");
		Client client3 = new Client("mdp6", "adresse@6", "nom6", "telephone6");
		Client client4 = new Client("mdp7", "adresse@7", "nom7", "telephone7");
		Client client5 = new Client("mdp8", "adresse@8", "nom8", "telephone8");
		Client client6 = new Client("mdp9", "adresse@9", "nom9", "telephone9");
		Client client7 = new Client("mdp10", "adresse@10", "nom10", "telephone10");
		Client client8 = new Client("mdp11", "adresse@11", "nom11", "telephone11");
		Client client9 = new Client("mdp12", "adresse@12", "nom12", "telephone12");
		
		client1.setLivraisonAdresses(clientAdresses);
		
		//client1.setClientAdresse(adresse1);
		
		Fournisseur fournisseur1 = new Fournisseur("mdp1", "adresse@1","nom1","siret1","tva1");
		Fournisseur fournisseur2 = new Fournisseur("mdp2", "adresse@2","nom2","siret2","tva2");
		Fournisseur fournisseur3 = new Fournisseur("mdp3", "adresse@3","nom3","siret3","tva3");
		
		Adresse adresse3 = new Adresse("nom3","prenom3","raisonSociale3","rue1 3","rue2 3",783690,"ville3");
		Adresse adresse4 = new Adresse("nom4","prenom4","raisonSociale4","rue1 4","rue2 4",783690,"ville4");
		
		fournisseur1.setFournisseurAdresse(adresse3);
		fournisseur2.setFournisseurAdresse(adresse4);
		
		Vendeur vendeur1 = new Vendeur("mdp13", "adresse13");
		Vendeur vendeur2 = new Vendeur("mdp14", "adresse14");
		
		Acheteur acheteur1 = new Acheteur("mdp15", "adresse15");
		Acheteur acheteur2 = new Acheteur("mdp16", "adresse16");
		
		Administrateur administrateur1 = new Administrateur("mdp17", "adresse17");
		Administrateur administrateur2 = new Administrateur("mdp18", "adresse18");
		
		Comptable comptable1 = new Comptable("mdp19", "adresse19");
		Comptable comptable2 = new Comptable("mdp20", "adresse20");
		
		//em.persist(membre1);
		em.persist(adresse1);
		em.persist(adresse2);
		
		em.persist(client1);
		em.persist(client2);
		em.persist(client3);
		em.persist(client4);
		em.persist(client5);
		em.persist(client6);
		em.persist(client7);
		em.persist(client8);
		em.persist(client9);
		
		em.persist(adresse3);
		em.persist(adresse4);
		
		em.persist(fournisseur1);
		em.persist(fournisseur2);
		em.persist(fournisseur3);
		
		em.persist(vendeur1);
		em.persist(vendeur2);
		
		em.persist(acheteur1);
		em.persist(acheteur2);
		
		em.persist(administrateur1);
		em.persist(administrateur2);
		
		em.persist(comptable1);
		em.persist(comptable2);
		
		//System.out.println("Persist OK");
		

		em.close();
	
	}

	@Transactional
	public void removeData() {
		runSql("\\src\\main\\resources\\removeData.sql");
	}

	@Transactional
	public void createData() {
		runSql("\\src\\main\\resources\\createData.sql");
	}
	
	public void runSql(String path) {
		
		String sqlCur = "";
		
		Properties prop = System.getProperties();
		String curDir = prop.getProperty("user.dir");
		
		try {
			
			BufferedReader br = new BufferedReader(new FileReader(curDir + path));
			String line;
			while ((line = br.readLine()) != null) {
				if(line.length() < 5)  continue;
				if(line.startsWith("--")) continue;
				
				if(! line.trim().endsWith(";")) {
					sqlCur = sqlCur.concat(line);
					continue;
				}
				if(sqlCur.length() > 0) {
					line = sqlCur + line;
					sqlCur = "";
				}
				Query query = em.createNativeQuery(line);
				query.executeUpdate();
			}
			br.close();
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}